package com.example.a1.menudialogtest_325cht;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tvinfo,tvmenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvinfo= (TextView) findViewById(R.id.textView);
        tvmenu= (TextView) findViewById(R.id.textView2);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //实例化inflater
        MenuInflater menuInflater = getMenuInflater();
        //动态加载menu
        menuInflater.inflate(R.menu.main,menu);


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //菜单点击事件
        switch (item.getItemId()){
            case R.id.menu1:
                tvinfo.setText("菜单1");
                break;

            case R.id.menu2:
                tvinfo.setText("菜单2");
                break;

            case R.id.menu3:
                tvinfo.setText("菜单3");
                showdialog();
                break;

            case R.id.menu4:
                tvinfo.setText("菜单4");

                break;

            case R.id.menu5:
                tvinfo.setText("菜单5");
                break;

            case R.id.menu6:
                tvinfo.setText("菜单6");
                showdialog();
                break;

            case R.id.menu7:
                tvinfo.setText("菜单7");
                break;

        }
        return super.onOptionsItemSelected(item);
    }
    private void showdialog(){
        final View view = getLayoutInflater().inflate(R.layout.dialoglayout,null);
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("对话框标题")
                .setIcon(R.mipmap.ic_launcher)
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText editText = (EditText)view.findViewById(R.id.editText);
                        String str = editText.getText().toString();
                        tvmenu.setText(str);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //取消按钮操作
                    }
                })
                .create();
        dialog.show();
    }
}
